package com.gwall.demo;

import com.gwall.mapper.UserMapper;
import com.gwall.model.UserPo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.boot.autoconfigure.security.SecurityProperties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @Author jiangxuzhao
 * @Description Mapper 代理方式
 * @Date 2024/6/14
 */
public class MybatisDemo2 {
    public static void main(String[] args) throws IOException {

        // 1. 创建 SqlSessionFactory
        String resource = "Mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 2. 获取 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 3. 获取 UserMapper 代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<UserPo> users = userMapper.getByIds(Arrays.asList(1, 2, 3));
        System.out.println(users);

        // 4. 释放资源
        sqlSession.close();
    }
}
